package bairui.international.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffMoneyRecord;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffRoyaltyRecord;
import bairui.international.model.domain.jpa.TB_BranchCompanyStaffWithdrawOrder;
import bairui.international.model.type.Type_BranchCompanyStaffMoneyRecord;
import bairui.international.service.Service_BranchCompanyStaff;
import bairui.international.service.Service_BranchCompanyStaffMoneyRecord;
import bairui.international.service.Service_BranchCompanyStaffRoyaltyRecord;
import bairui.international.service.Service_BranchCompanyStaffWithdrawOrder;

/**
 * 
 * 【(分公司员工金额记录)TB_BranchCompanyStaffMoneyRecord】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_BranchCompanyStaffMoneyRecord 模型
 * @see Service_BranchCompanyStaffMoneyRecord 服务接口
 * @see BService_BranchCompanyStaffMoneyRecordImpl 服务实现基础
 *
 */
@Service
public class Service_BranchCompanyStaffMoneyRecordImpl extends BService_BranchCompanyStaffMoneyRecordImpl implements Service_BranchCompanyStaffMoneyRecord {

	@Autowired private Service_BranchCompanyStaffWithdrawOrder service_BranchCompanyStaffWithdrawOrder;
	@Autowired private Service_BranchCompanyStaffRoyaltyRecord service_BranchCompanyStaffRoyaltyRecord;
	@Autowired private Service_BranchCompanyStaff service_BranchCompanyStaff;
	
	private TB_BranchCompanyStaffMoneyRecord createInstance(
		Double money, TB_BranchCompanyStaff branchCompanyStaff, Type_BranchCompanyStaffMoneyRecord type,
		TB_BranchCompanyStaffWithdrawOrder userWithdrawOrder, 
		TB_BranchCompanyStaffRoyaltyRecord userRoyaltyRecord,
		boolean flag
	) {
		// 设置总金额
		TB_BranchCompanyStaff userAmount = service_BranchCompanyStaff.findByIdUseLock(branchCompanyStaff.getId());
		double currentMoney = BigDecimal
				.valueOf(userAmount.getMoney())
				.add(BigDecimal.valueOf(money))
				.doubleValue();
		userAmount.setMoney(currentMoney);
		//
		double _currentWithdrawMoney = userAmount.getWithdrawMoney();
		if(flag){
			_currentWithdrawMoney = BigDecimal
					.valueOf(_currentWithdrawMoney)
					.add(BigDecimal.valueOf(money))
					.doubleValue();
			userAmount.setWithdrawMoney(_currentWithdrawMoney);
		}
		double currentWithdrawMoney = _currentWithdrawMoney;
		//
		return dao.save(new TB_BranchCompanyStaffMoneyRecord(
				new TB_BranchCompanyStaffMoneyRecord.MustProperties() {
					@Override public Double getMoney() {
						return money;
					}
					@Override public Double getCurrentMoney() {
						return currentMoney;
					}
					@Override public Type_BranchCompanyStaffMoneyRecord getType() {
						return type;
					}
					@Override public TB_BranchCompanyStaff getBranchCompanyStaff() {
						return branchCompanyStaff;
					}
					@Override public Double getCurrentWithdrawMoney() {
						return currentWithdrawMoney;
					}
				},
				new TB_BranchCompanyStaffMoneyRecord.OptionalSingleProperties() {
					@Override public TB_BranchCompanyStaffWithdrawOrder getBranchCompanyStaffWithdrawOrder() {
						return userWithdrawOrder;
					}
					@Override public TB_BranchCompanyStaffRoyaltyRecord getBranchCompanyStaffRoyaltyRecord() {
						return userRoyaltyRecord;
					}
				}
			));
	}

	/**
	 * 提现
	 */
	@Override
	public TB_BranchCompanyStaffMoneyRecord create(TB_BranchCompanyStaffWithdrawOrder userWithdrawOrder) {
		userWithdrawOrder = service_BranchCompanyStaffWithdrawOrder.load(userWithdrawOrder);
		//
		return createInstance(
				-userWithdrawOrder.getMoney(), 
				userWithdrawOrder.getBranchCompanyStaff(), 
				Type_BranchCompanyStaffMoneyRecord.Withdraw, 
				userWithdrawOrder,
				null,
				true
			);
	}
	
	/**
	 * 提现退回
	 */
	@Override
	public TB_BranchCompanyStaffMoneyRecord createReturn(TB_BranchCompanyStaffWithdrawOrder userWithdrawOrder) {
		userWithdrawOrder = service_BranchCompanyStaffWithdrawOrder.load(userWithdrawOrder);
		//
		return createInstance(
				userWithdrawOrder.getMoney(), 
				userWithdrawOrder.getBranchCompanyStaff(), 
				Type_BranchCompanyStaffMoneyRecord.WithdrawReturn, 
				userWithdrawOrder,
				null,
				true
			);
	}

	/**
	 * 提成
	 */
	@Override
	public TB_BranchCompanyStaffMoneyRecord create(TB_BranchCompanyStaffRoyaltyRecord userRoyaltyRecord) {
		userRoyaltyRecord = service_BranchCompanyStaffRoyaltyRecord.load(userRoyaltyRecord);
//		boolean flag = false;
//		if(userRoyaltyRecord.getProductOrder().getPayWay() == Type_ProductOrderPay.OnLine){
//			flag = true;
//		}
		//
		return createInstance(
				userRoyaltyRecord.getMoney(), 
				userRoyaltyRecord.getBranchCompanyStaff(), 
				Type_BranchCompanyStaffMoneyRecord.Royalty, 
				null,
				userRoyaltyRecord,
				true
			);
	}

}
